//
//  YCLWelcomeViewController.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/12.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class YCLWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //设置UI
        prepareUI()
        
        //在网络请求之前判断是否有用户信息
        setIcon()
        
        
        //发送网络请求获取用户数据
        YCLUserAccountViewModel.sharedUserAccountViewModel.getUserInfo { (error) in
            
            if error != nil{
            
                YCLPrint("获取用户信息失败")
                return
            }
            
//            YCLPrint("获取用户信息成功")
            self.setIcon()
        }
        

    }
    
    private func setIcon() {
    
        //给欢迎界面设置头像
        if let URLString = YCLUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_large{
            
            let url = NSURL.init(string: URLString)
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage.init(named: "avatar_default_big"))
        }

        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        //改变头像控件的约束
        iconView.snp_updateConstraints { (make) in
            
            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height - 160))
        }
        
        //initialSpringVelocity初速度
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue:0), animations: {
            //通知系统重新布局
            self.view.layoutIfNeeded()
            
            }) { (_) in
             
                //让label显示出来
                UIView.animateWithDuration(1, delay: 0, options: UIViewAnimationOptions(rawValue:0), animations: { 
                    
                    self.describeLabel.alpha = 1
                    }, completion: { (_) in
                        
//                        YCLPrint("跳到微博信息界面")
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchViewController(YCLTabBarController())
                })
                
        }
    }
    
    func prepareUI() {
        
        //添加子控件
        view.addSubview(bgView)
        view.addSubview(iconView)
        view.addSubview(describeLabel)
        
        //设置约束
        
        bgView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        describeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        bgView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        iconView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(-160)
            
            make.size.equalTo(CGSize(width: 85,height: 85))
        }
        
        describeLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        
        
    }
    
    //MARK 懒加载
    //背景图片
    private lazy var bgView: UIImageView = UIImageView.init(image: UIImage.init(named: "ad_background"))
    //头像图片
    private lazy var iconView:UIImageView = {
    
        let icon = UIImageView.init(image: UIImage.init(named: "avatar_default_big"))
        
        icon.layer.cornerRadius = 42.5
        icon.layer.masksToBounds = true
        
        
        return icon
    }()
    //描述文字
    private lazy var describeLabel:UILabel = {
    
        let label = UILabel()
        label.text = "欢迎回来"
//        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(16)
        label.alpha = 0
        label.sizeToFit()
        
        return label
    }()
    

}
